local extension = Package:new("YS__fontaine")
extension.extensionName = "genshin"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
     ["YS__fontaine"] = "○神-枫丹",
     ["YS"] = "○神",
}

---@param room Room @ 房间
---@return integer[]
local function GetRenPile(room)
  room.tag["ren"] = room.tag["ren"] or {}
  return table.simpleClone(room.tag["ren"])
end

local function NotifyRenPile(room)
  room:sendLog{
    type = "#NotifyRenPile",
    arg = #GetRenPile(room),
    card = GetRenPile(room),
  }
  room:setBanner("@$RenPile", table.simpleClone(room.tag["ren"]))
  --room:doBroadcastNotify("ShowToast", Fk:translate("RenPileToast")..table.concat(table.map(GetRenPile(room), function(id) return Fk:getCardById(id):toLogString() end), "、"))
end

---@param room Room @ 房间
---@param card integer|integer[]|Card|Card[] @ 要加入仁区的牌/id/intList
---@param skillName string @ 移动的技能名
---@param proposer integer @ 移动操作者的id
local function AddToRenPile(room, card, skillName, proposer)
  local ids = Card:getIdList(card)
  room.tag["ren"] = room.tag["ren"] or {}
  local ren_cards = table.simpleClone(room.tag["ren"])
  if #ids + #ren_cards <= 6 then
    table.insertTable(room.tag["ren"], ids)
  else
    local rens = {}
    if #ids >= 6 then
      for i = 1, 6, 1 do
        table.insert(rens, ids[i])
      end
    else
      local n = #ids + #ren_cards - 6
      for i = n + 1, #ren_cards, 1 do
        table.insert(rens, ren_cards[i])
      end
      table.insertTable(rens, ids)
    end
    room.tag["ren"] = rens
  end
  local dummy = Fk:cloneCard("dilu")
  dummy:addSubcards(table.filter(ids, function(id) return table.contains(room.tag["ren"], id) end))
  room:moveCardTo(dummy, Card.Void, nil, fk.ReasonJustMove, skillName, nil, true, proposer)
  room:sendLog{
    type = "#AddToRenPile",
    arg = #dummy.subcards,
    card = dummy.subcards,
  }

  local dummy2 = Fk:cloneCard("dilu")
  dummy2:addSubcards(table.filter(ren_cards, function(id) return not table.contains(room.tag["ren"], id) end))
  if #dummy2.subcards > 0 then
    room:moveCardTo(dummy2, Card.DiscardPile, nil, fk.ReasonJustMove, "ren_overflow", nil, true, nil)
    room:sendLog{
      type = "#OverflowFromRenPile",
      arg = #dummy2.subcards,
      card = dummy2.subcards,
    }
  end

  NotifyRenPile(room)
end

---@param room Room @ 房间
---@param player ServerPlayer @ 获得牌的角色
---@param cid integer|Card @ 要获得的牌/id
---@param skillName string @ 技能名
local function GetCardFromRenPile(room, player, cid, skillName)
  skillName = skillName or ""
  if type(cid) ~= "number" then
    cid = cid:isVirtual() and cid.subcards or {cid.id}
  else
    cid = {cid}
  end
  if #cid == 0 then return end
  local move = {
    ids = cid,
    to = player.id,
    toArea = Card.PlayerHand,
    moveReason = fk.ReasonJustMove,
    proposer = player.id,
    moveVisible = true,
    skillName = skillName,
  }
  room.logic:trigger("fk.BeforeRenMove", nil, move)
  room:moveCards(move)
  room.logic:trigger("fk.AfterRenMove", nil, move)
  for _, id in ipairs(cid) do
    table.removeOne(room.tag["ren"], id)
  end
  room:sendLog{
    type = "#GetCardFromRenPile",
    from = player.id,
    arg = #cid,
    card = cid,
  }
  NotifyRenPile(room)
end

---@param room Room @ 房间
---@param player ServerPlayer @ 弃置牌的角色
---@param ids integer|integer[] @ 要弃置的id/idList
---@param skillName string @ 技能名
local function DiscardCardFromRenPile(room, player, ids, skillName)
  skillName = skillName or ""
  if type(ids) ~= "number" then
    ids = ids
  else
    ids = {ids}
  end
  if #ids == 0 then return end
  local move = {
    ids = ids,
    toArea = Card.DiscardPile,
    moveReason = fk.ReasonDiscard,
    proposer = player.id,
    moveVisible = true,
    skillName = skillName,
  }
  room.logic:trigger("fk.BeforeRenMove", nil, move)
  room:moveCards(move)
  room.logic:trigger("fk.AfterRenMove", nil, move)
  for _, id in ipairs(ids) do
    table.removeOne(room.tag["ren"], id)
  end
  room:sendLog{
    type = "#DiscardCardFromRenPile",
    from = player.id,
    arg = #ids,
    card = ids,
  }
  NotifyRenPile(room)
end

Fk:loadTranslationTable{
  ["#NotifyRenPile"] = "“仁”区现有 %arg 张牌 %card",
  ["RenPileToast"] = "仁区：",
  ["#AddToRenPile"] = "%arg 张牌被移入“仁”区 %card",
  ["#OverflowFromRenPile"] = "%arg 张牌从“仁”区溢出 %card",
  ["#GetCardFromRenPile"] = "%from 从“仁”区获得 %arg 张牌 %card",
  ["#DiscardCardFromRenPile"] = "%from 弃置了“仁”区 %arg 张牌 %card",
  ["$RenPile"] = "仁区",
  ["@$RenPile"] = "仁区",
}








local YS__nwlt = General(extension, "YS__nwlt", "Hydro", 4, 4, General.Male)
local YS__nwltwin = fk.CreateActiveSkill{ name = "YS__nwlt_win_audio" }
YS__nwltwin.package = extension
Fk:addSkill(YS__nwltwin)
local YuS = {{"yuanshui", Card.Spade, 1},{"yuanshui", Card.Spade, 2},{"yuanshui", Card.Spade, 3},{"yuanshui", Card.Spade, 4},{"yuanshui", Card.Spade, 5},{"yuanshui", Card.Spade, 6},{"yuanshui", Card.Spade, 7}}
local YS__leichang = fk.CreateTriggerSkill{
name = "YS__leichang",
anim_type = "special",
events = {fk.CardUsing, fk.CardResponding},
can_trigger = function(self, event, target, player, data)
  local room = player.room
  local cardnum = player:getHandcardNum()
  local card1 = table.filter(U.prepareDeriveCards(room, YuS, "YuS"), function (id)
    return room:getCardArea(id) == Card.Void or room:getCardArea(id) == Card.DiscardPile or room:getCardArea(id) == Card.DrawPile
  end)
  return target == player and player:hasSkill(self) and
  cardnum%2 == 1 and #card1 ~= 0
end,
on_cost = function(self, event, target, player, data)
  local room = player.room
  if room:askForSkillInvoke(player, self.name, data) then
    return true
  end
  self.cancel_cost = true
end,
on_use = function(self, event, player, target, data)
  local room = player.room
  local cards = table.filter(U.prepareDeriveCards(room, YuS, "YuS"), function (id)
    return room:getCardArea(id) == Card.Void or room:getCardArea(id) == Card.DiscardPile or room:getCardArea(id) == Card.DrawPile
  end)
  local get = table.random(cards)
  if get and player.hp > math.floor(player.maxHp / 2) then
  room:loseHp(player, 1)
  player:addToPile("yuanshui", get, true, self.name)
  elseif player.hp <= math.floor(player.maxHp / 2) then
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name
    })
  end
end,
}
local YS__chaoshuiTrigger = fk.CreateTriggerSkill{
name = "YS__chaoshuiTrigger",
mute = true,
events = {fk.TurnStart},
can_trigger = function(self, event, target, player, data)
    return #player:getPile("yuanshui") > 0 and player:getMark("@chaoshui") == 0
  end,
on_cost = function(self, event, target, player, data)
  local room = player.room
  if room:askForSkillInvoke(player, self.name, data, "#YS__chaoshui_invoke::"..target.id) then
    return true
  end
  end,
on_use = function(self, event, target, player, data)
  local room = player.room
  local choice = {}
    for i = 1, 2*#room.alive_players do
      if i == 9 then break end
      table.insert(choice, ""..i)
    end
  local num = player.room:askForChoice(player, choice, self.name, "#YS__chaoshui_invoke::"..target.id)
  room:notifySkillInvoked(player, "YS__chaoshuiTrigger")
  player:broadcastSkillInvoke("YS__chaoshuiTrigger", 7)
  player.room:setPlayerMark(player, "@chaoshui", num .. "-" .. num)
end,

refresh_events = {fk.TurnStart},
can_refresh = function(self, event, target, player, data)
  return player:getMark("@chaoshui") ~= 0 and target == player and player:hasSkill(self)
end,
on_refresh = function(self, event, target, player, data)
  local room = player.room
  local nums = string.split(player:getMark("@chaoshui"), "-")
  local num = tonumber(nums[1])
  local choice = {"Cancel"}
    for i = 1, num do
      table.insert(choice, ""..i)
    end
  local c = room:askForChoice(player, choice, "YS__chaoshuiTrigger", "调整当前施法标记")
  if c ~= "Cancel" then
  local num2 = c
  room:setPlayerMark(player, "@chaoshui", num .. "-" .. num2)
  end
end,
}
local chaoshui_conjure = fk.CreateTriggerSkill{
     name = "#chaoshui_conjure",
     mute = true,
     events = {fk.TurnEnd},
     can_trigger = function(self, event, target, player, data)
       return player:getMark("@chaoshui") ~= 0 and string.sub(player:getMark("@chaoshui"), -1) == "0"
     end,
     on_cost = function() return true end,
     on_use = function(self, event, target, player, data)
       local room = player.room
       local nums = string.split(player:getMark("@chaoshui"), "-")
       local num = tonumber(nums[1])
       room:setPlayerMark(player, "@chaoshui", 0)
       if #player:getPile("yuanshui") > 0 and target ~= player then
         room:notifySkillInvoked(player, "YS__chaoshuiTrigger")
         if num > 2 then
          player:broadcastSkillInvoke("YS__chaoshuiTrigger", math.random(1,3))
         else
          player:broadcastSkillInvoke("YS__chaoshuiTrigger", math.random(4,6))
         end
         local slash = Fk:cloneCard "thunder__slash"
          while true do
          if target.dead then break end
          if #player:getPile("yuanshui") == 0 then break end
          room:moveCards({
            from = player.id,
            ids = {Fk:getCardById(player:getPile("yuanshui")[1]).id},
            toArea = Card.DrawPile,
            moveReason = fk.ReasonJustMove,
            skillName = self.name,
            drawPilePosition = math.random(1,#room.draw_pile)
          })
          if player:isWounded() then
            room:recover({
              who = player,
              num = 1,
              recoverBy = player,
              skillName = self.name
            })
          end
         slash.skillName = self.name
         room:useCard{
          from = player.id,
          tos = {{target.id}},
          card = slash,
          extraUse = true,
          }
          room:addPlayerMark(player, "cscscs-turn", 1)
          if player:getMark("cscscs-turn") == num then break end
         end
       end
     end,
   
     refresh_events = {fk.TurnEnd},
     can_refresh = function(self, event, target, player, data)
       return player:getMark("@chaoshui") ~= 0
     end,
     on_refresh = function(self, event, target, player, data)
       local room = player.room
       local nums = string.split(player:getMark("@chaoshui"), "-")
       local num = tonumber(nums[1])
       local num2 = tonumber(nums[2]) - 1
       if num2 < 0 then
        room:setPlayerMark(player, "@chaoshui", num .. "-" .. 0)
      else
        room:setPlayerMark(player, "@chaoshui", num .. "-" .. num2)
      end
     end,
}
local YS__leichangYuS = fk.CreateTriggerSkill{
  name = "#YS__leichangYuS",
  anim_type = "special",
  events = {fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    return target == player and player:hasSkill(self) and
    data.card.name == "yuanshui"
  end,
  on_cost = function(self, event, target, player, data)
      return true
  end,
  on_use = function(self, event, player, target, data)
    player:addToPile("yuanshui", data.card, true, self.name)
  end,
  }
YS__nwlt:addSkill(YS__leichang)
YS__leichang:addRelatedSkill(YS__leichangYuS)
YS__nwlt:addSkill(YS__chaoshuiTrigger)
YS__chaoshuiTrigger:addRelatedSkill(chaoshui_conjure)
Fk:loadTranslationTable{
     ["YS__nwlt"] = "那维莱特",
     ["YS__chaoshuiTrigger"] = "潮水",
     [":YS__chaoshuiTrigger"] = "所有角色的回合开始时，若你的武将牌上有【源水之滴】且未施法，则你可以施法X：将武将牌上一张【源水之滴】随机置入牌堆并回复一点体力视为对当前回合角色（为你自己则施法失败）使用一张雷【杀】，然后你重复此流程直到当前回合角色死亡或你的武将牌上没有【源水之滴】或以此法使用了X张【杀】。<br/>你的回合开始时，若你已施法则你可以重新调整当前施法标记。（X至少为1且至多为当前场上存活角色数的2倍但上限为8）<br/><font color='grey'>#\"<b>施法</b>\"<br/>一名角色的回合结束前，施法标记-1，减至0时执行施法效果。施法期间不能重复施法同一技能。",
     ["#chaoshui_conjure"] = "潮水",
     ["YS__leichang"] = "泪偿",
     [":YS__leichang"] = "当你使用或打出牌后，若你的手牌数为奇数：①你的体力值大于上限的一半，则你可以失去一点体力并从游戏外或牌堆或弃牌堆中随机获得一张【源水之滴】将其置于你的武将牌上；②你的体力值不大于上限的一半，则你可以回复一点体力。你使用【源水之滴】后将其置于你的武将牌上。（初始7张）"..
      "<br/><font color='grey'>#\"<b>源水之滴</b>\"：基本牌，出牌阶段对自己使用，然后你摸一张牌并回复一点体力。",
     ["#YS__leichangYuS"] = "泪偿",
     ["#YS__chaoshui_invoke"] = "潮水：施法，从%dest的回合开始，第X个回合结束前，释放衡平推裁！。",
     ["@chaoshui"] = "<font color='#46A3FF'>潮水</font>",
     ["$YS__chaoshuiTrigger1"] = "凡高大者，我无不蔑视。",
     ["$YS__chaoshuiTrigger2"] = "化作，浮沫吧。",
     ["$YS__chaoshuiTrigger3"] = "潮水啊，我已归来。",
     ["$YS__chaoshuiTrigger4"] = "不得喧哗。",
     ["$YS__chaoshuiTrigger5"] = "低头。",
     ["$YS__chaoshuiTrigger6"] = "肃静！",
     ["$YS__chaoshuiTrigger7"] = "我将对诸神行使裁决，没有任何事情可以改变这点。",
     ["~YS__nwlt"] = "潮汐再起时，我必将归来…",
     ["$YS__nwlt_win_audio"] = "从僭位者那里回收了七之大权一角，这才是我本来的身姿。我已是「完全之龙」。",
}
local YS__xld = General(extension, "YS__xld", "Cryo", 4, 4, General.Female)
local YS__xldwin = fk.CreateActiveSkill{ name = "YS__xld_win_audio" }
YS__xldwin.package = extension
Fk:addSkill(YS__xldwin)
local YS__qvjing = fk.CreateTriggerSkill{
  name = "YS__qvjing",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.RoundStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      return target == player and player:hasSkill(self) and data.card.type ~= Card.TypeEquip and data.firstTarget and (not table.contains(AimGroup:getAllTargets(data.tos), player.id) or data.card.name == "amazing_grace" or data.card.name == "god_salvation" or data.card.name == "iron_chain")
    else
      return player:hasSkill(self)
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil)
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
    if #AimGroup:getAllTargets(data.tos) == 0 then return end
    for _, id in ipairs(AimGroup:getAllTargets(data.tos)) do
      local t = player.room:getPlayerById(id)
      if t ~= player and t:getMark("@YSjianying") == 0 then
        player.room:addPlayerMark(t, "@YSjianying", 2)
      elseif t ~= player and t:getMark("@YSjianying") == 1 then
        player.room:addPlayerMark(t, "@YSjianying", 1)
      end
    end
    else
      local targets = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), function(p)
        return p.id end), 0, 2, "令至多两名角色获得“剪影”", self.name, true, false)
        for _, id in ipairs(targets) do
          local t = player.room:getPlayerById(id)
          if t ~= player and t:getMark("@YSjianying") == 0 then
            player.room:addPlayerMark(t, "@YSjianying", 2)
          elseif t ~= player and t:getMark("@YSjianying") == 1 then
            player.room:addPlayerMark(t, "@YSjianying", 1)
          end
        end
    end
  end,

  refresh_events = {fk.TurnEnd, fk.Damaged},
  can_refresh = function(self, event, target, player, data)
    return target ~= player and target:getMark("@YSjianying") ~= 0 and player:hasSkill(self)
  end,
  on_refresh = function(self, event, target, player, data)
  local room = player.room
    local num = target:getMark("@YSjianying")
    local cards = table.filter(target:getCardIds("ehj"), function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
    if num == 1 then
      if #cards > 0 then
        room:notifySkillInvoked(player, "YS__qvjing")
        AddToRenPile(room, table.random(cards, 2), self.name, player.id)
      end
    else
      room:notifySkillInvoked(player, "YS__qvjing")
      AddToRenPile(room, table.random(cards, 1), self.name, player.id)
    end
      player.room:addPlayerMark(target, "@YSjianying", -1)
end,
}
local YS__dingge = fk.CreateActiveSkill{
  name = "YS__dingge",
  anim_type = "support",
  card_num = 0,
  target_num = 0,
  expand_pile = function ()
    return Self:getTableMark("$RenPile")
  end,
  can_use = function(self, player)
    return #player:getTableMark("$RenPile") > 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local from = room:getPlayerById(effect.from)
    local cards = GetRenPile(room)
    local dummy = Fk:cloneCard("dilu")
    dummy:addSubcards(cards)
    GetCardFromRenPile(room, from, dummy, self.name)
    local move = room:askForYiji(from, cards, room.alive_players, self.name, 1, #cards, nil, nil, false)
    room:doYiji(move, from.id, self.name)
    local victims = {}
    for p, c in pairs(move) do
      if #c > 0 then
        table.insert(victims, tonumber(p))
      end
    end
    room:sortPlayersByAction(victims)
    for _, pid in ipairs(victims) do
      local p = room:getPlayerById(pid)
      if not p.dead then
        room:recover({
          who = p,
          num = 1,
          recoverBy = from,
          skillName = self.name
        })
      end
    end
  end,
}
local YS__dinggeTrig = fk.CreateTriggerSkill{
name = "#YS__dinggeTrig",
mute = true,
frequency = Skill.Compulsory,
refresh_events = {fk.StartPlayCard},
can_refresh = function(self, event, target, player, data)
  return target == player and player:hasSkill(self)
end,
on_refresh = function(self, event, target, player, data)
  local room = player.room
  room:setPlayerMark(player, "$RenPile", U.GetRenPile(room))
end,
}
YS__xld:addSkill(YS__qvjing)
YS__xld:addSkill(YS__dingge)
YS__dingge:addRelatedSkill(YS__dinggeTrig)
Fk:loadTranslationTable{
  ["YS__xld"] = "夏洛蒂",
  ["YS__qvjing"] = "取景",
  [":YS__qvjing"] = "当你使用牌指定其他角色为目标时，你可以令所有目标获得2层“剪影”（上限为2）。每轮开始时，你可以令至多两名其他角色获得2层“剪影”。"..
  "<br/><font color='grey'>#\"<b>剪影</b>\"：有“剪影”的角色回合结束或受到伤害时，将区域内随机一张牌置入仁区然后移除一层“剪影”。如果是最后一层，则改为置入两张牌。",
  ["@YSjianying"] = "<font color='#95CACA'>剪影</font>",
  ["YS__dingge"] = "定格",
  [":YS__dingge"] = "出牌阶段，若仁区里有牌，则你可以分配仁区里所有牌，以此法被分配到牌的角色各回复一点体力。",
}

local YS__fnn = General(extension, "YS__fnn", "Hydro", 4, 4, General.Female)
local YS__fnnwin = fk.CreateActiveSkill{ name = "YS__fnn_win_audio" }
YS__fnnwin.package = extension
Fk:addSkill(YS__fnnwin)
local YS__kuanghuan = fk.CreateActiveSkill{
  name = "YS__kuanghuan",
  mute = true,
  anim_type = "support",
  min_target_num = 1,
  card_num = 0,
  prompt = "#YS__kuanghuan",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards)
    return true
  end,
  feasible = function (self, selected, selected_cards)
    return #selected > 0
  end,
  on_use = function(self, room, effect)
  local player = room:getPlayerById(effect.from)
  local tos = effect.tos
  if #tos ~= 1 then
    room:sortPlayersByAction(tos)
    local plushp = 0
    for _, pid in ipairs(tos) do
      local p = room:getPlayerById(pid)
      if not p.dead then
        plushp = plushp + p.hp
      end
    end
    local d = 0
    for _, pid in ipairs(tos) do
      local p = room:getPlayerById(pid)
      if not p.dead then
        local num = math.abs(p.hp - math.min(p.maxHp, math.floor(plushp/#effect.tos)))
        p.hp = math.min(p.maxHp, math.floor(plushp/#effect.tos))
        room:broadcastProperty(p, "hp")
        room:setPlayerMark(p, "@YS__qf", num)
        if num ~= 0 then
          d = 1
          room:setPlayerMark(p, "@@YS__ht", 1)
        end
      end
    end
    if d == 0 then
      room:loseHp(player, 1, self.name)
      player.general = "YS__fnn"
      room:broadcastProperty(player, "general")
      player:broadcastSkillInvoke("YS__kuanghuan", math.random(3,4))
      for _, pid in ipairs(tos) do
        local p = room:getPlayerById(pid)
        room:damage{
          from = player,
          to = p,
          damage = 1,
          damageType = 328,
          skillName = self.name,
        }
      end
    else
      player:broadcastSkillInvoke("YS__kuanghuan", math.random(1,2))
    end
  elseif #tos == 1 then
    local p = room:getPlayerById(effect.tos[1])
    player.general = "YS__fnn_w"
    room:broadcastProperty(player, "general")
    player:broadcastSkillInvoke("YS__kuanghuan", math.random(5,6))
    if p.maxHp < 10 then
    room:changeMaxHp(p, 1)
    end
    room:recover({
      who = p,
      num = 1,
      recoverBy = player,
      skillName = self.name
    })
  end
  end,
}
local YS__ht = fk.CreateTriggerSkill{
  name = "#YS__ht",
  frequency = Skill.Compulsory,
  events = {fk.HpChanged, fk.DamageCaused, fk.PreHpRecover},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@YS__ht") ~= 0
  end,
  on_cost = function(self, event, target, player, data)
      return true
    end,
  on_use = function(self, event, target, player, data)
    if event == fk.HpChanged then
      player.room:addPlayerMark(player, "@YS__qf", math.abs(data.num))
    elseif event == fk.DamageCaused then
      data.damage = data.damage + math.floor(player:getMark("@YS__qf")/2)
    elseif event == fk.PreHpRecover then
      data.num = data.num + math.floor(player:getMark("@YS__qf")/2)
    end
  end,
}
local YS__yuanwu = fk.CreateTriggerSkill{
  name = "YS__yuanwu",
  frequency = Skill.Compulsory,
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    local availableTargets = table.map(table.filter(room.alive_players, function(p) 
      return p:getMark("@YS__qf") ~= 0
    end), function(p)
      return p.id
    end)
    return target == player and #availableTargets ~= 0 and player:hasSkill(self)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local availableTargets = table.map(table.filter(room.alive_players, function(p) 
      return p:getMark("@YS__qf") ~= 0
    end), function(p)
      return p.id
    end)
    if #availableTargets == 0 then return false end
    local sum = 0
    for _, targetId in ipairs(availableTargets) do
      sum = sum + room:getPlayerById(targetId):getMark("@YS__qf")
      room:setPlayerMark(room:getPlayerById(targetId), "@YS__qf", 0)
      room:setPlayerMark(room:getPlayerById(targetId), "@@YS__ht", 0)
    end
    local num = player.maxHp + math.floor(sum/2)
    if num <= 10 then
    room:changeMaxHp(player, math.floor(sum/2))
    end
  end,
}
YS__fnn:addSkill(YS__kuanghuan)
YS__kuanghuan:addRelatedSkill(YS__ht)
YS__fnn:addSkill(YS__yuanwu)
Fk:loadTranslationTable{
  ["YS__fnn"] = "芙宁娜",
  ["YS__fnn_w"] = "芙宁娜",
  ["YS__kuanghuan"] = "狂欢",
  ["#YS__kuanghuan"] = "狂欢:选择任意名角色提供增益",
  ["#YS__ht"] = "欢腾",
  [":YS__kuanghuan"] = "出牌阶段限一次，你可以选择任意名角色，然后将这些角色的体力值调整为平均值（向下取整），体力值因此变化的角色获得等量“气氛”并进入欢腾状态直到你下回合开始；<br/>/ / 若未有角色体力值因此变化，则你流失一点体力并对这些角色各造成一点水元素伤害；<br/>/ / 若只选择了一名角色，则改为令其增加一点体力上限并回复一点体力。（至多加到10上限）"..
  "<br/><font color='grey'>#\"<b>欢腾</b>\"：体力值变化时，获得等量气氛；每2点气氛令该角色造成的所有伤害和受到的回复效果+1。",
  ["YS__yuanwu"] = "圆舞",
  [":YS__yuanwu"] = "锁定技，回合开始时，回收场上所有气氛，每2点气氛为你提供一点体力上限。(至多加到10上限)",
  ["@YS__qf"] = "气氛",
  ["@@YS__ht"] = "<font color='#46A3FF'>欢腾</font>",
}
local YS__fnn_w = General(extension, "YS__fnn_w", "Hydro", 3)
YS__fnn_w.total_hidden = true

local YS__losl = General(extension, "YS__losl", "Cryo", 4, 4, General.Male)
local YS__loslwin = fk.CreateActiveSkill{ name = "YS__losl_win_audio" }
YS__loslwin.package = extension
Fk:addSkill(YS__loslwin)
local YS__shuangquan = fk.CreateActiveSkill{
  name = "YS__shuangquan",
  anim_type = "offensive",
  card_num = 2,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.TrueFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    if player.dead then return end
    room:throwCard(effect.cards, self.name, player)
    if player.dead then return end
    room:setPlayerMark(player, "@@YS__shuangchi", 1)
    room:addPlayerMark(player, MarkEnum.SlashResidue, 2)
  end,
}
local YS__shuangchi_filter = fk.CreateFilterSkill{
  name = "#YS__shuangchi_filter",
  mute = true,
  card_filter = function(self, card, player, isJudgeEvent)
    return player:hasSkill("YS__shuangquan") and card.trueName == "slash" and player:getMark("@@YS__shuangchi") ~= 0 and
    (table.contains(player.player_cards[Player.Hand], card.id) or isJudgeEvent)
  end,
  view_as = function(self, card)
    return Fk:cloneCard("ice__slash", card.suit, card.number)
  end,
}
local losl_mashu = fk.CreateDistanceSkill{
  name = "#losl__mashu",
  frequency = Skill.Compulsory,
  correct_func = function(self, from, to)
    if from:hasSkill(self) and from:getMark("@@YS__shuangchi") ~= 0 then
      return -1
    end
  end,
}
local YS__shuangchi = fk.CreateTriggerSkill{
  name = "#YS__shuangchi",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.PreDamage},
  priority = 2,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@YS__shuangchi") ~= 0 and data.card and data.card.trueName == "slash"
  end,
  on_cost = function(self, event, target, player, data)
      return true
    end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.hp > player.maxHp/2 and event == fk.PreDamage then
      room:loseHp(player, 1, self.name)
      data.damage = data.damage + 1
    elseif player.hp <= player.maxHp/2 and event == fk.PreDamage then
      room:recover({
        who = player,
        num = 2,
        recoverBy = player,
        skillName = self.name
      })
      player:drawCards(2)
      room:setPlayerMark(player, "@@YS__shuangchi", 0)
      room:addPlayerMark(player, MarkEnum.SlashResidue, -2)
      if room.current == player or data.to == room.current then
      room:endTurn()
      end
    end
  end,
}
local YS__shuangchi_end = fk.CreateTriggerSkill{
  name = "#YS__shuangchi_end",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    return target == player and player:getMark("@@YS__shuangchi") ~= 0 and player:hasSkill(self)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@YS__shuangchi", 0)
    room:addPlayerMark(player, MarkEnum.SlashResidue, -2)
  end,
}
local YS__chengcai = fk.CreateTriggerSkill{
  name = "YS__chengcai",
  frequency = Skill.Compulsory,
  mute = true,
  events = {fk.HpChanged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = function(self, event, target, player, data)
      return true
    end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.HpChanged then
      room:addPlayerMark(player, "@YS__chengjie", 1)
      if player:getMark("@YS__chengjie") >= 2 then
        room:removePlayerMark(player, "@YS__chengjie", 2)
        local slash = Fk:cloneCard("ice__slash")
        slash.skillName = self.name
        if player:prohibitUse(slash) then return false end
        local max_num = slash.skill:getMaxTargetNum(player, slash)
        if max_num == 0 then return false end
        local targets = {}
        for _, p in ipairs(room.alive_players) do
          if not (p == player or player:isProhibited(p, slash)) then
            table.insert(targets, p.id)
          end
        end
        if #targets == 0 then return false end
        local tos = room:askForChoosePlayers(player, targets, 1, max_num, "#chengcai-slash:::" .. max_num, self.name, true, true)
        if #tos > 0 then
          player:broadcastSkillInvoke("YS__chengcai", math.random(1,3))
          room:useCard({
            from = player.id,
            tos = table.map(tos, function(pid) return { pid } end),
            card = slash,
          })
        else
          player:drawCards(1)
        end
      end
    end
  end,
}
local YS__hanyi = fk.CreateTriggerSkill{
  name = "YS__hanyi",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.PreDamage},
  priority = -1,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    return target == player and player:hasSkill(self) and not data.to:isNude() and data.damageType == fk.IceDamage
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ice_damage_skill = Fk.skills["ice_damage_skill"]
    room:useSkill(player, ice_damage_skill, function()
      return ice_damage_skill:use(fk.DamageCaused, target, player, {to = data.to})
    end)
    return true
  end,
}
YS__losl:addSkill(YS__shuangquan)
YS__shuangquan:addRelatedSkill(YS__shuangchi_filter)
YS__shuangquan:addRelatedSkill(losl_mashu)
YS__shuangquan:addRelatedSkill(YS__shuangchi)
YS__shuangquan:addRelatedSkill(YS__shuangchi_end)
YS__losl:addSkill(YS__chengcai)
YS__losl:addSkill(YS__hanyi)
Fk:loadTranslationTable{
  ["YS__losl"] = "莱欧斯利",
  ["YS__shuangquan"] = "霜拳",
  ["#YS__shuangchi"] = "霜拳",
  ["#YS__shuangchi_filter"] = "霜拳",
  ["#YS__shuangchi_end"] = "霜拳",
  [":YS__shuangquan"] = "出牌阶段限一次，你可以弃置两张牌然后进入“霜驰”状态直到你下回合开始。"..
  "<br/><font color='grey'>#\"<b>霜驰</b>\"：所有【杀】视为冰【杀】，出杀次数+2且与其他角色距离-1；当你使用【杀】即将造成伤害时，若你的体力值大于上限的一半，则你流失一点体力令此伤害+1；否则你回复两点体力摸两张牌并退出“霜驰”；若此时是你的回合或目标为当前回合角色，则结束当前回合。",
  ["@@YS__shuangchi"] = "霜驰",
  ["YS__chengcai"] = "惩裁",
  [":YS__chengcai"] = "锁定技，体力值变化时，获得一点“惩戒”；当你拥有2点“惩戒”时，移除所有“惩戒”视为使用一张无距离限制的冰【杀】,否则你摸一张牌。",
  ["@YS__chengjie"] = "惩戒",
  ["#chengcai-slash"] = "惩裁：你可以视为使用冰【杀】，选择%arg名角色为目标",
  ["YS__hanyi"] = "寒意",
  [":YS__hanyi"] = "锁定技，当你造成冰冻伤害前，触发冰伤效果"
}

local YS__ln = General(extension, "YS__ln", "Pyro", 3, 3, General.Male)
local YS__lnwin = fk.CreateActiveSkill{ name = "YS__ln_win_audio" }
YS__lnwin.package = extension
Fk:addSkill(YS__lnwin)
local YS__xunyou = fk.CreateTriggerSkill{
  name = "YS__xunyou",
  anim_type = "drawcard",
  mute = true,
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    local suit = ""
    if data.card.suit == Card.Spade then
      suit = "YS__xunyouJLspade"
    elseif data.card.suit == Card.Heart then
      suit = "YS__xunyouJLheart"
    elseif data.card.suit == Card.Club then
      suit = "YS__xunyouJLclub"
    elseif data.card.suit == Card.Diamond then
      suit = "YS__xunyouJLdiamond"
    end
    return math.min(player:getMark("YS__xunyouJLspade"),player:getMark("YS__xunyouJLheart"),player:getMark("YS__xunyouJLclub"),player:getMark("YS__xunyouJLdiamond")) == player:getMark(""..suit) and target == player and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, "YS__xunyou")
    player:drawCards(1, self.name)
    player:broadcastSkillInvoke(self.name, math.random(1,2))
    if math.min(player:getMark("YS__xunyouJLspade"),player:getMark("YS__xunyouJLheart"),player:getMark("YS__xunyouJLclub"),player:getMark("YS__xunyouJLdiamond")) == math.max(player:getMark("YS__xunyouJLspade"),player:getMark("YS__xunyouJLheart"),player:getMark("YS__xunyouJLclub"),player:getMark("YS__xunyouJLdiamond")) then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
      data.additionalDamage = (data.additionalDamage or 0) + 1
      room:addPlayerMark(player, "YS__xifa-turn", 1)
    end 
  end,

  refresh_events = {fk.AfterCardsMove, fk.TurnEnd},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self) then
    if event == fk.AfterCardsMove then
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        return true
      end  
    for _, info in ipairs(move.moveInfo) do
      if info.fromArea == Card.DiscardPile and player.room:getCardArea(info.cardId) ~= Card.DiscardPile then
        return true
      end
    end
    end
    else
      return true
    end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
    for _, move in ipairs(data) do
        for _, info in ipairs(move.moveInfo) do
         local suit = Fk:getCardById(info.cardId):getSuitString()
         local Pcards = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(room.discard_pile, info.cardId) and Fk:getCardById(info.cardId):getSuitString() == suit then
              table.insertIfNeed(Pcards, info.cardId)
            end
          end
        end
      end
    end, Player.HistoryTurn)
         if table.contains({ "spade", "heart", "club", "diamond" }, suit) then
          room:setPlayerMark(player, "YS__xunyouJL"..Fk:getCardById(info.cardId):getSuitString(), #Pcards)
         end
      end
    end
    room:setPlayerMark(player, "@", string.format("%d♠ %d<font color='red'>♥</font> %d♣ %d<font color='red'>♦</font>",
    player:getMark("YS__xunyouJLspade"),
    player:getMark("YS__xunyouJLheart"),
    player:getMark("YS__xunyouJLclub"),
    player:getMark("YS__xunyouJLdiamond")
  ))
  else
    room:setPlayerMark(player, "@", 0)
    room:setPlayerMark(player, "YS__xunyouJLspade", 0)
    room:setPlayerMark(player, "YS__xunyouJLheart", 0)
    room:setPlayerMark(player, "YS__xunyouJLclub", 0)
    room:setPlayerMark(player, "YS__xunyouJLdiamond", 0)
  end
  end,
}
local YS__xunyou_buff = fk.CreateTargetModSkill{
  name = "#YS__xunyou_buff",
  mute = true,
  residue_func = function(self, player, skill, scope, card)
    local suit = ""
    if card.suit == Card.Spade then
      suit = "YS__xunyouJLspade"
    elseif card.suit == Card.Heart then
      suit = "YS__xunyouJLheart"
    elseif card.suit == Card.Club then
      suit = "YS__xunyouJLclub"
    elseif card.suit == Card.Diamond then
      suit = "YS__xunyouJLdiamond"
    end
    return (math.max(player:getMark("YS__xunyouJLspade"),player:getMark("YS__xunyouJLheart"),player:getMark("YS__xunyouJLclub"),player:getMark("YS__xunyouJLdiamond")) == player:getMark(""..suit) and card and card.suit ~= Card.NoSuit) and 998 or 0
  end,
}

local YS__xifa = fk.CreateActiveSkill{
  name = "YS__xifa",
  anim_type = "drawcard",
  card_num = 1,
  target_num = 0,
  times = function(self)
    return Self.phase == Player.Play and Self:getMark("YS__xifa-turn")
  end,
  can_use = function(self, player)
    return player:getMark("@") ~= 0 and player:getMark("YS__xifa-turn") ~= 0
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local card = effect.cards
    local Pcards = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(room.discard_pile, info.cardId) then
              table.insertIfNeed(Pcards, info.cardId)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    local c = room:askForCardsChosen(player, player, 1, 1, {card_data = {{self.name, Pcards}}}, self.name)
    room:moveCardTo(Fk:getCardById(c[1]), Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, false, player.id)
    room:moveCardTo(card, Card.DiscardPile, player, fk.ReasonJustMove, self.name, nil, false, player.id)
    room:loseHp(player, 1)
    room:addPlayerMark(player, "YS__xifa-turn", -1)
  end,
}
YS__ln:addSkill(YS__xunyou)
YS__xunyou:addRelatedSkill(YS__xunyou_buff)
YS__ln:addSkill(YS__xifa)
Fk:loadTranslationTable{
  ["YS__ln"] = "林尼",
  ["YS__xunyou"] = "巡游",
  [":YS__xunyou"] = "你使用花色为本回合进入弃牌堆最少的牌时，你可以摸一张牌；<br/>使用花色为本回合进入弃牌堆最多的牌时，无次数限制。<br/>同时满足，则你可以恢复一点体力并令此牌造成的伤害+1，然后本回合你可以额外使用一次戏法。",
  ["YS__xifa"] = "戏法",
  [":YS__xifa"] = "出牌阶段限零次，你可以用一张牌替换本回合进入弃牌堆的一张牌，如此做，你失去一点体力。",
}
return extension